package com.glsc.ngateway.platform.service.glres;

import cn.hutool.core.collection.CollectionUtil;
import com.glsc.ngateway.common.api.common.utils.PageUtil;
import com.glsc.ngateway.common.api.platform.dto.user.PlatformUserDto;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.glres.GlscSystemResource;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.glres.GlscSystemTeam;
import com.glsc.ngateway.common.base.repo.mysql.gateway.platform.glres.GlscSystemTeamRepository;
import com.glsc.ngateway.platform.service.UserService;
import com.glsc.ngateway.platform.utils.SpecificationUtil;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

@Service
public class GlscSystemTeamService {

    @Resource
    private GlscSystemTeamRepository glscSystemTeamRepository;

    @Resource
    private UserService userService;

    @Resource
    private GlscSystemResService glscSystemResService;

    /**
     * 分页查找
     *
     * @param pageNo
     * @param pageSize
     * @param param
     * @return
     */
    public Page<GlscSystemTeam> page(int pageNo, int pageSize, Map<String, Object> param) {
        Sort.Direction direction = Sort.Direction.DESC;
        String sort = "createTime";

        pageNo = PageUtil.initPageNo(pageNo);
        pageSize = PageUtil.initPageSize(pageSize);

        Pageable pageable = PageRequest.of(pageNo - 1, pageSize, direction, sort);
        Page<GlscSystemTeam> page = glscSystemTeamRepository.findAll(SpecificationUtil.buildSpecification(param, GlscSystemTeam.class), pageable);
        return page;
    }

    /**
     * 根据系统id查找所有的团队成员信息
     *
     * @param systemId
     * @return
     */
    public List<GlscSystemTeam> listBySystemId(Integer systemId) {
        return glscSystemTeamRepository.findAll(Example.of(GlscSystemTeam.builder().systemId(systemId).build()));
    }


    /**
     * 根据id查找
     *
     * @param id
     * @return
     */
    public GlscSystemTeam findById(Integer id) {
        Optional<GlscSystemTeam> optional = glscSystemTeamRepository.findById(id);
        if (optional.isPresent()) {
            return optional.get();
        }
        return null;
    }

    public List<GlscSystemTeam> findBySysIdAndUserType(Integer systemId, String userType) {
        List<GlscSystemTeam> teams = glscSystemTeamRepository.findAll(Example.of(GlscSystemTeam.builder().systemId(systemId)
                .userType(userType).build()));
        return teams;
    }

    /**
     * 保存
     *
     * @param obj
     * @return
     */
    @Transactional
    public GlscSystemTeam save(GlscSystemTeam obj) {
        PlatformUserDto user = userService.findByUsername(obj.getUserOaAccount());
        obj.setUserId(user.getId());
        obj.setUserName(user.getName());

        List<GlscSystemTeam> oldTeams = glscSystemTeamRepository.findAll(Example.of(GlscSystemTeam.builder().systemId(obj.getSystemId())
                .userOaAccount(obj.getUserOaAccount()).build()));
        if (CollectionUtil.isNotEmpty(oldTeams) && oldTeams.get(0).getId() != obj.getId()) {
            Assert.isTrue(false, "该团队成员已存在");
        }

        if ("manageA".equals(obj.getUserType())) {
            GlscSystemResource system = glscSystemResService.findById(obj.getSystemId());
            system.setSystemManage(user.getName());
            system.setSystemManageOaAccount(user.getUsername());

            List<GlscSystemTeam> teams = findBySysIdAndUserType(obj.getSystemId(), "manageA");
            if (CollectionUtil.isNotEmpty(teams) && !teams.get(0).getUserOaAccount().equals(obj.getUserOaAccount())) {
                GlscSystemTeam glscSystemTeam = teams.get(0);
                glscSystemTeam.setDeleted("Y");
                glscSystemTeamRepository.save(glscSystemTeam);

            }
        }

        return glscSystemTeamRepository.save(obj);
    }

    /**
     * 根据id删除
     *
     * @param id
     */
    public void delete(Integer id) {
        glscSystemTeamRepository.deleteById(id);
    }
}
